//
//  ViewController.m
//  UIWebView
//
//  Created by 王恕 on 15/12/8.
//  Copyright © 2015年 王恕. All rights reserved.
//

#import "ViewController.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"

@interface ViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>

@property (nonatomic,strong) UIButton *goBackBtn;
@property (nonatomic,strong) UIButton *goForwardBtn;
@property (nonatomic,strong) UIButton *reLoadBtn;
@property (nonatomic,strong) UIButton *stopLoadingBtn;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) NJKWebViewProgressView *_progressView;
@property (nonatomic,strong) NJKWebViewProgress *_progressProxy;

@end

@implementation ViewController

@synthesize button,goBackBtn,goForwardBtn,reLoadBtn,stopLoadingBtn,webView,_progressProxy,_progressView;

BOOL flag;

- (void)viewDidLoad {
    [super viewDidLoad];
    _progressProxy = [[NJKWebViewProgress alloc] init];
    
    
    CGFloat progressBarHeight = 2.f;
    CGRect barFrame = CGRectMake(0, 50, 375, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    // _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self baidu];
    [self btn];
}

- (void)btn{
    
    flag = NO;
    CGRect bounds = CGRectMake(0,50, self.view.bounds.size.width, self.view.bounds.size.height - 150);
    //设置切换按钮
    button = [[UIButton alloc]initWithFrame:CGRectMake(bounds.size.width/3, bounds.size.height+60, bounds.size.width/3, 90)];
    button.backgroundColor = [UIColor grayColor];
    [button setTitle:@"网络" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    //设置前进后退按钮
    goBackBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, bounds.size.height+60, bounds.size.width/3, 90)];
    goBackBtn.backgroundColor = [UIColor grayColor];
    [goBackBtn setTitle:@"回退" forState:UIControlStateNormal];
    [goBackBtn addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    
    goForwardBtn = [[UIButton alloc]initWithFrame:CGRectMake(2*bounds.size.width/3, bounds.size.height+60, bounds.size.width/3, 90)];
    goForwardBtn.backgroundColor = [UIColor grayColor];
    [goForwardBtn setTitle:@"前进" forState:UIControlStateNormal];
    [goForwardBtn addTarget:self action:@selector(goForward:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:goBackBtn];
    [self.view addSubview:goForwardBtn];
    
    //设置刷新和取消载入按钮
    reLoadBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, bounds.size.width/2, 50)];
    reLoadBtn.backgroundColor = [UIColor grayColor];
    [reLoadBtn setTitle:@"刷新" forState:UIControlStateNormal];
    [reLoadBtn addTarget:self action:@selector(reLoad:) forControlEvents:UIControlEventTouchUpInside];
    
    stopLoadingBtn = [[UIButton alloc]initWithFrame:CGRectMake(bounds.size.width/2, 0, bounds.size.width/2, 50)];
    stopLoadingBtn.backgroundColor = [UIColor grayColor];
    [stopLoadingBtn setTitle:@"取消" forState:UIControlStateNormal];
    [stopLoadingBtn addTarget:self action:@selector(stopLoading:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:reLoadBtn];
    [self.view addSubview:stopLoadingBtn];
    
    
}

- (void)baidu{
    
    //1.创建UIWebView
    CGRect bounds = CGRectMake(0,50, self.view.bounds.size.width, self.view.bounds.size.height - 150);
    webView = [[UIWebView alloc]initWithFrame:bounds];
    
    //2.设置属性
    webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
    [webView setUserInteractionEnabled:YES];//是否支持交互
    //    webView.delegate = self;//实现代理
    webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    //网络资源
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];//创建url（统一资源定位符，互联网标准资源的地址）
    NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url];//创建NSURLRequest
    [webView loadRequest:urlRequest];//加载
    //6.显示WebView
    [self.view addSubview:webView];
    
}

//button用来切换本地资源和网络资源
-(void)change{
    if (flag) {
        flag = NO;
        [button setTitle:@"网络" forState:UIControlStateNormal];
        [button setTitle:@"本地" forState:UIControlStateHighlighted];
        
        //网络资源
        NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];//创建url（统一资源定位符，互联网标准资源的地址）
        NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url];//创建NSURLRequest
        [webView loadRequest:urlRequest];//加载
    }else{
        flag = YES;
        [button setTitle:@"本地" forState:UIControlStateNormal];
        [button setTitle:@"网络" forState:UIControlStateHighlighted];
        
        //本地资源
        NSString* filePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"jpg"];//本地资源路径
        NSURL* localUrl = [NSURL fileURLWithPath:filePath];//创建本地资源URL
        NSURLRequest* request = [NSURLRequest requestWithURL:localUrl];//创建NSURLRequest
        [webView loadRequest:request];//加载
        
    }
    
}

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view addSubview:_progressView];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // Remove progress view
    // because UINavigationBar is shared with other ViewControllers
    [_progressView removeFromSuperview];
}

//回退功能实现
-(void)goBack: (NSString *) goBackbtn{
    
    [webView goBack];
}
//前进功能实现
-(void)goForward: (NSString *) goForwardbtn{
    
    [webView goForward];
    
}
//刷新功能实现
-(void)reLoad: (NSString *) reLoadbtn{
    
    [webView reload];
    
}
//停止加载功能实现
-(void)stopLoading: (NSString *) stopLoadingbtn{
    
    [webView stopLoading];
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    return  YES;//YES if the web view should begin loading content; otherwise, NO .
    
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"webView已经开始加载");
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"webView结束加载");
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
    NSLog(@"加载失败的信息：%@",error);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
